/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sahd.broker.iONetMessageManager;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Observer;

/**
 *
 * @author Romms
 */
public class IONetMessageManager {
    private OutgoingNetMessageManager outgoingNetMessageManager;
    private IncomingNetMessageManager incomingNetMessageManager;

    private static IONetMessageManager instance;

    public static IONetMessageManager getInstance(Observer observer,int outPort,
            int entryPort, String initialServerIpAddress) throws UnknownHostException, IOException{
        if(instance == null){
            instance = new IONetMessageManager(observer,outPort,entryPort,initialServerIpAddress);
        }
        return instance;
    }

    public IONetMessageManager(Observer observer, int outPort, int entryPort,
            String initialServerIpAddress) throws UnknownHostException, IOException{
        incomingNetMessageManager=new IncomingNetMessageManager(observer,entryPort);
        outgoingNetMessageManager=new OutgoingNetMessageManager(initialServerIpAddress,outPort);
    }

    public void shipMessage(Object content, int protocol) throws IOException{
        this.outgoingNetMessageManager.sendMessage(content, protocol);
    }

    public void shipMessage(Object content, int protocol, int serviceNameRequestedProtocol) throws IOException{
        this.outgoingNetMessageManager.sendMessage(content, protocol,serviceNameRequestedProtocol);
    }

    public void shipMessage(Object content, int protocol, String originalSourceIp, int serviceRequestedProtocol) throws IOException {
        this.outgoingNetMessageManager.sendMessage(content, protocol,originalSourceIp,serviceRequestedProtocol );
    }

    public void setReceiverIpAddres(String ipAddres) throws UnknownHostException, IOException{
        outgoingNetMessageManager.setReceiverIpAddress(ipAddres);
    }

    public String getServerIpAddress(){
        return this.outgoingNetMessageManager.getServerIpAddress();
    }


    public void setEntryPort(int entryPort){
        this.incomingNetMessageManager.setEntryPort(entryPort);
    }

    public int getOutPort(){
        return this.outgoingNetMessageManager.getOutPort();
    }

    public void setOutPort(int outPort){
        this.outgoingNetMessageManager.setOutPort(outPort);
    }

    public void setInputPort(int entryPort){
        this.incomingNetMessageManager.setEntryPort(entryPort);
    }

    public int getInputPort(){
        return this.incomingNetMessageManager.getEntryPort();
    }

    public void startReceivingMessages(){
        if(!this.incomingNetMessageManager.isIsReceivinMessages()){
            this.incomingNetMessageManager.startReceivingMessages();
        }
    }

    public void stopReceivingMessages(){
        this.incomingNetMessageManager.stopReceivingMessages();
    }

    public void addObserver(Observer observer){
        incomingNetMessageManager.addObserver(observer);
    }

    public void removeObserver(Observer observer){
        this.incomingNetMessageManager.deleteObserver(observer);
    }


}
